package com.zxy.study.ui.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.provider.MediaStore
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.postDelayed
import com.zxy.study.R
import kotlinx.android.synthetic.main.activity_easy_image.*
import kotlinx.android.synthetic.main.layout_indicator_demo.*
import pl.aprilapps.easyphotopicker.DefaultCallback
import pl.aprilapps.easyphotopicker.EasyImage
import pl.aprilapps.easyphotopicker.MediaFile
import pl.aprilapps.easyphotopicker.MediaSource

/**
 * @author : Shonn.Zhang
 * @email  : shonn.zhang@mart.blue
 * @time   : 2020/12/2 11:41
 * @desc   :
 */
class EasyImageActivity : AppCompatActivity(), View.OnClickListener {

    companion object {
        const val TYPE_GALLERY = 1
        const val TYPE_CAMERA = 2
        const val TYPE_DOCUMENT = 3
        const val TYPE_CHOOSE_GALLERY = 4
        const val TYPE_CHOOSE_DOCUMENT = 5

        const val REQUEST_CODE_VIDEO = 100
    }

    private lateinit var easyImage: EasyImage

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_easy_image)

        btn_gallery.setOnClickListener(this)
        btn_camera_image.setOnClickListener(this)
        btn_document.setOnClickListener(this)
        btn_camera_video.setOnClickListener(this)
        btn_chooser.setOnClickListener(this)
        btn_video.setOnClickListener(this)

        easyImage = EasyImage.Builder(this).build()

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btn_gallery -> chooseGallery()
            R.id.btn_camera_image -> chooseCameraImage()
            R.id.btn_camera_video -> chooseCameraVideo()
            R.id.btn_document -> chooseDocument()
            R.id.btn_chooser -> chooseChooser()
            R.id.btn_video -> chooseVideo()
        }
    }

    private fun chooseVideo() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, REQUEST_CODE_VIDEO)
    }

    private fun chooseDocument() {
        easyImage.openDocuments(this)
    }

    private fun chooseGallery() {
        easyImage.openGallery(this)
    }

    private fun chooseCameraVideo() {
        easyImage.openCameraForVideo(this)
    }

    private fun chooseCameraImage() {
        easyImage.openCameraForImage(this)
    }

    private fun chooseChooser() {
        easyImage.openChooser(this)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == REQUEST_CODE_VIDEO && resultCode == Activity.RESULT_OK) {
            var result = data?.data
            Log.i("AAA", result.toString())
            return
        }

        easyImage.handleActivityResult(requestCode, resultCode, data, this, object : DefaultCallback() {

            override fun onImagePickerError(error: Throwable, source: MediaSource) {
                error.printStackTrace()
            }

            override fun onMediaFilesPicked(imageFiles: Array<MediaFile>, source: MediaSource) {
                Log.i("AAA", imageFiles[0].file.absolutePath)
            }

        })
    }


}